using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using IBS.Model;

namespace CommonApp
{
    public class PaymentXmlBuilder
    {
        public string PaymentDetailHtml(Payment paymentForDetail, string cardNumber)
        {
            var payment = XDocument.Parse("<payment>" + paymentForDetail.PaymentDetailTemplate + "</payment>");
            var res = from field in payment.Descendants("field")
                      select new XElement("div", new XAttribute("style", "padding: 5px; margin: 5px"),
                                          new XElement("label", field.Attribute("display").Value),
                                          new XElement("input", new XAttribute("value", field.Value),
                                                       new XAttribute("type", "text"),
                                                        new XAttribute("error", field.Attribute("error").Value),
                                                       new XAttribute("regexp", field.Attribute("regexp").Value)));

            var sBuilder = new StringBuilder();
            res.ToList().ForEach(s => sBuilder.Append(s));
            sBuilder.Append("<div style='padding: 5px; margin: 5px'><label id='validateSummary' style='color:red'/></div>");
            sBuilder.AppendFormat("<input type='hidden' name='paymentId' value='{0}' />", paymentForDetail.Id);
            sBuilder.AppendFormat("<input type='hidden' name='card' value='{0}'/>", cardNumber);
            sBuilder.AppendFormat("<span>Currency</span><input type='text' name='currency' />");
            sBuilder.AppendFormat("<span>Amount</span><input type='text' name='amount' />");
            sBuilder.Append("<input type='submit' onclick='return validate();'/>");

            return sBuilder.ToString();
        }
        public string PaymentsTreeXml(IEnumerable<Payment> payments)
        {
            StringBuilder stringBuilder = new StringBuilder("<xml>");
            payments.Select(p => "<payment>" + p.PaymentDetailTemplate + "<id>" + p.Id + "</id></payment>").ToList().ForEach(p => stringBuilder.Append(p));
            stringBuilder.Append("</xml>");
            var paymentsXml = stringBuilder.ToString();
            var eripPayments = XDocument.Parse(paymentsXml);
            var result = from i in eripPayments.Descendants("payment")
                         group i by new { company = i.Element("company").Value } into g
                         select new XElement("company", new XAttribute("text", g.Key.company),
                         from p in g
                         select new XElement("payment", new XAttribute("id", p.Element("id").Value),
                             new XAttribute("text", p.Element("title").Value)));
            var sBuilder = new StringBuilder();
            sBuilder.Append("<xml>");
            foreach (var xElement in result)
            {
                sBuilder.Append(xElement);
            }
            sBuilder.Append("</xml>");
            return sBuilder.ToString();
        }
    }
}
